package com.serviceprovider.business.utility.maintenance.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.report.valueobjects.BatchVO;
import com.serviceprovider.business.module.sp.transaction.model.Batch;
import com.serviceprovider.business.module.sp.transaction.model.TransactionDetail;
import com.serviceprovider.business.module.sp.transaction.model.TransactionStatus;
import com.serviceprovider.business.module.sp.transaction.model.TransactionUserAmount;
import com.serviceprovider.business.module.sp.transaction.services.dao.interfaces.IBatchDao;
import com.serviceprovider.business.module.sp.transaction.services.dao.interfaces.ITransStatusDao;
import com.serviceprovider.business.module.sp.transaction.services.dao.interfaces.ITransactionDao;
import com.serviceprovider.business.module.sp.transaction.services.dao.interfaces.ITransactionUserAmountDao;
import com.serviceprovider.business.module.sp.transaction.valueobjects.TransactionDetailsVO;
import com.serviceprovider.business.module.user.model.impl.User;
import com.serviceprovider.business.utility.maintenance.interfaces.IBatchService;
import com.serviceprovider.business.utility.maintenance.settings.AppSettings;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//~--- JDK imports ------------------------------------------------------------

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class BatchService implements IBatchService {
    @Autowired
    private IBatchDao batchDAO;

//  @Autowired
//    private IProviderDao providerDAO;
//  @Autowired
//    private ProviderService providerService;
    @Autowired
    private ITransStatusDao           transStatusDao;
    @Autowired
    private ITransactionDao           transactionDao;
    @Autowired
    private ITransactionUserAmountDao transactionUserAmountDao;

    @Override
    public void save(BatchVO batchVO, User provider) {
        Calendar cal   = Calendar.getInstance();
        Batch    batch = new Batch(batchVO);

        batch.setBatchDate(cal.getTime());
        batchDAO.insertBatch(batch);

        for (TransactionDetailsVO tr : batchVO.getTransactions().values()) {
            TransactionDetail trans = new TransactionDetail();

            trans.setUserId(provider.getId());
            trans.setBatch(batch);

            TransactionStatus status = transStatusDao.get(AppSettings.TRANSACTION_STATUS_APPROVED_ID);

            trans.setTransactionStatus(status);
            transactionDao.insertTransaction(trans);
        }
    }

    @Override
    public List<BatchVO> getBatchList() {
        List<BatchVO> batchVOList = new ArrayList<BatchVO>();
        List<Batch>   batchs      = batchDAO.findAll();

        // FIXME: [Performance] should do a bulk loading Transaction
        for (Batch batch : batchs) {
            List<TransactionDetail> transactions = transactionDao.findTransactionsByBatch(batch.getId());

            for (TransactionDetail trans : transactions) {
                trans.setBatch(batch);

                List<TransactionUserAmount> transAmount = transactionUserAmountDao.findByTransactionDetail(batch,
                                                              trans);

                trans.setTransactionUserAmounts(transAmount);
            }

            batch.setTransactionDetails(transactions);

            BatchVO batchVO = new BatchVO(batch);

            batchVOList.add(batchVO);
        }

        return batchVOList;
    }

    @Override
    public List<BatchVO> getBatchListToPlaceOrder(List<Integer> batchIds) {
        List<BatchVO> batchVOList = new ArrayList<BatchVO>();
        List<Batch>   batchs      = batchDAO.findToPlaceOrder(batchIds);

        // FIXME: [Performance] should do a bulk loading Transaction
        for (Batch batch : batchs) {
            List<TransactionDetail> transactions = transactionDao.findTransactionsByBatch(batch.getId());

            for (TransactionDetail trans : transactions) {
                trans.setBatch(batch);

                List<TransactionUserAmount> transAmount = transactionUserAmountDao.findByTransactionDetail(batch,
                                                              trans);

                trans.setTransactionUserAmounts(transAmount);
            }

            batch.setTransactionDetails(transactions);

            BatchVO batchVO = new BatchVO(batch);

            batchVO.setIncludedInOrder(true);
            batchVOList.add(batchVO);
        }

        return batchVOList;
    }

    @Override
    public void setOrderId(BatchVO batchVO, Integer id) {
        Batch b = new Batch(batchVO);

        b.setOrderId(id);
        batchDAO.updateBatch(b);
    }

    @Override
    public List<BatchVO> getBatchListByOrderId(Integer orderId) {
        List<BatchVO> batchVOList = new ArrayList<BatchVO>();
        List<Batch>   batchs      = batchDAO.findBatchByOrder(orderId);

        for (Batch batch : batchs) {
            List<TransactionDetail> transactions = transactionDao.findTransactionsByBatch(batch.getId());

            for (TransactionDetail trans : transactions) {
                trans.setBatch(batch);

                List<TransactionUserAmount> transAmount = transactionUserAmountDao.findByTransactionDetail(batch,
                                                              trans);

                trans.setTransactionUserAmounts(transAmount);
            }

            batch.setTransactionDetails(transactions);

            BatchVO batchVO = new BatchVO(batch);

            batchVOList.add(batchVO);
        }

        return batchVOList;
    }
}



